package com.htandroid.htandroid.views;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Scroller;

/**
 * Created by Lyn
 */

public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {

    private float mLastY = -1; // save event y
    private Scroller mScroller;

    private LoadMoreListViewFooter mLoadMoreListViewFooter;
    private boolean mIsFooterReady = false;

    private LoadMoreListener mListViewListener;
    private int mTotalItemCount;

    private final static int SCROLL_DURATION = 400;
    private final static int PULL_LOAD_MORE_DELTA = 84;
    private final static float OFFSET_RADIO = 1.8f;

    private int mState;

    public LoadMoreListView(Context context) {
        super(context);
        initWithContext(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initWithContext(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initWithContext(context);
    }

    private void initWithContext(Context context) {
        mScroller = new Scroller(context, new DecelerateInterpolator());
        super.setOnScrollListener(this);
        mState = LoadMoreListViewFooter.STATE_NORMAL;
        mLoadMoreListViewFooter = new LoadMoreListViewFooter(context);
        mLoadMoreListViewFooter.hide();
        mLoadMoreListViewFooter.setState(LoadMoreListViewFooter.STATE_NORMAL);
        mLoadMoreListViewFooter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startLoadMore();
            }
        });
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        if (mIsFooterReady == false) {
            mIsFooterReady = true;
            addFooterView(mLoadMoreListViewFooter);
        }
        super.setAdapter(adapter);
    }


    private void startLoadMore() {
        if (mListViewListener != null) {
            mListViewListener.onLoadMore();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mLastY == -1) {
            mLastY = ev.getRawY();
        }

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastY = ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float deltaY = ev.getRawY() - mLastY;
                mLastY = ev.getRawY();
                if (getLastVisiblePosition() == mTotalItemCount - 1 &&
                        (mLoadMoreListViewFooter.getBottomMargin() > 0 ||
                                deltaY < 0)) {
                    updateFooterHeight(-deltaY / OFFSET_RADIO);
                }
                break;
            default:
                if (getLastVisiblePosition() == mTotalItemCount - 1) {
                    if (mLoadMoreListViewFooter.getBottomMargin() > PULL_LOAD_MORE_DELTA) {
                        startLoadMore();
                    }
                    resetFooterHeight();
                }
                break;
        }
        return super.onTouchEvent(ev);
    }


    private void updateFooterHeight(float delta) {
        int height = mLoadMoreListViewFooter.getBottomMargin() + (int) delta;
        if (height > PULL_LOAD_MORE_DELTA) {
            mLoadMoreListViewFooter.setState(LoadMoreListViewFooter.STATE_READY);
        } else {
            mLoadMoreListViewFooter.setState(LoadMoreListViewFooter.STATE_NORMAL);
        }
        mLoadMoreListViewFooter.setBottomMargin(height);
        mLoadMoreListViewFooter.setPulling(true);
        // setSelection(mTotalItemCount - 1); // scroll to bottom
    }

    private void resetFooterHeight() {
        int bottomMargin = mLoadMoreListViewFooter.getBottomMargin();
        if (bottomMargin > 0) {
            mLoadMoreListViewFooter.setState(mState);
            mLoadMoreListViewFooter.setPulling(false);
            mScroller.startScroll(0, bottomMargin, 0, -bottomMargin, SCROLL_DURATION);
            invalidate();
        }
    }

    public void setLoadEnable() {
        mState = LoadMoreListViewFooter.STATE_NORMAL;
        mLoadMoreListViewFooter.setState(mState);
        mLoadMoreListViewFooter.show();
    }

    public void setLoadNoMore() {
        mState = LoadMoreListViewFooter.STATE_NOMORE;
        mLoadMoreListViewFooter.setState(mState);
        mLoadMoreListViewFooter.hide();
    }
    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            mLoadMoreListViewFooter.setBottomMargin(mScroller.getCurrY());
            postInvalidate();
        }
        super.computeScroll();
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        mTotalItemCount = i2;
    }

    public void setOnLoadMoreListener(LoadMoreListener loadMoreListener) {
        mListViewListener = loadMoreListener;
    }

    public interface LoadMoreListener {
        public void onLoadMore();
    }
}
